#include <QCoreApplication>
#include <iostream>
using namespace std;

class Base8{
public :
    //  没有方法体的方法称为纯虚方法
    //  纯虚方法所在的类必须是抽象的
    // 保证子类 一定具有某些行为方法
    virtual void work();
};

int main(int argc, char *argv[])
{
    Base8 b;
    b.work();
    return 0;
}



class AbstractCalculator{
public:
    AbstractCalculator(int a,int b){
        this->m_A = a;
        this->m_B = b;
    }
    virtual int getResult() = 0;
    int m_A =1;
    int m_B =2;
    static AbstractCalculator* buildFacotry(  string op ){
        if(op =="+"){
            return new AddCalculator();
        }else if(op =="-"){
            return new AddCalculator();
        }
    }
};
class AddCalculator:public AbstractCalculator {
public:
    AddCalculator():AbstractCalculator(0,0){}
    AddCalculator(int a, int b):AbstractCalculator(a,b){}
    int getResult () override{
        return this->m_A +this->m_B ;
    }
};

class SubCalculator:public AbstractCalculator {
public:
    SubCalculator(int a, int b):AbstractCalculator(a,b){}
    int getResult () override{
        return this->m_A -  this->m_B ;
    }
};
class MulCalculator:public AbstractCalculator {
public:int main7(int argc, char *argv[])
    {
        int a =1;
        int b =2;
        AbstractCalculator *c =   AbstractCalculator::buildFacotry("+");
        int z =  c->getResult();
        cout<< "z:" << z <<endl;
        return 0;
    }


    MulCalculator(int a, int b):AbstractCalculator(a,b){}
public:
    int getResult () override{
        return this->m_A *  this->m_B ;
    }
};



//  对同一个类的不同对象的相同方法调用返回不同 的结果,这种现象称为多态
// 多态的实现方式：方法重载与方法重写
// 实现方法重写
//  定义虚方法：virtual 通知编译器此方法可以被子类重写，但是子类可以不使用override
//  重写虚方法：override 通知编译器此方法的目的是重写父类的虚方法，父类必须声明为virtual
//  使用final的虚方法可以防止子类重写父类
//  final; class  Base5 final{}; 不允许被继承的类称为final类
 class  Base5 {
public:
    virtual  void work(){
        cout<< "Base5 work" <<endl;
    }

    virtual void  constfun()    {
        this->m_A++;
    }

    void drive(){
    }
    void drive(int ){
    }
private :
    int m_A =1;
};

class Base6: public Base5{
public:
    void  constfun(){

    }
    void work() override{
        cout<< "Base6 work" <<endl;
    }
};
class Base7: public Base5{
public:
    void work() override{
        cout<< "Base7 work" <<endl;
    }
};

int main5(int argc, char *argv[])
{
    Base5 *b1 = new Base6;
    Base5 *b2 = new Base7;
    b1->work();
    b2->work();
    return 0;
}


int main4(int argc, char *argv[])
{
    Base5 b1;
    b1.work();
    Base5 b2;
    b2.work();
    Base5 b3;
    b3.work();
    return 0;
}






class Animal{
public:
    int m_A =1;
};

class Dog:public Animal{
public :
    int m_B=2;
};
class Cat:public Animal{
public :
    int m_C=3;
};
class Base4:public Dog, public Cat{

};
int main3(int argc, char *argv[])
{
    Base4 b;
    return 0;
}



class Base3{
public:
    static  Base3* createInstance(){
        cout << "createInstance" <<endl;
        if(instance== nullptr){
            instance =  new Base3();
        }
        return instance;
    }
    static  Base3* instance;
private:
    Base3(){
        cout << "Base3" <<endl;
    }
};
Base3* Base3::instance= nullptr;
int main2(int argc, char *argv[])
{
    Base3* b =  Base3::createInstance();
    b =  Base3::createInstance();
    b =  Base3::createInstance();
    // 在类外无法访问私有构造器，导致实例化失败
    // Base3 b1;
    // Base3 b2;
    // Base3 *b3 =new Base3();
    // Base3 b3;
    // b3.age =10;
    return 0;
}



class Base1{

public:
    Base1( int a ) {
        cout << "Base1" << endl;
    }
private:
    int m_A =0;

};
class Base2{
public:
    Base2(int c ){
        cout << "Base2" << endl;
    }
private:
    int m_C=3;

};
// 子类继承父类的顺序决定了父类对象的实例化顺序；
// 默认情况下，子类总是调用父类的默认无参构造器

class Son:public Base2,public Base1{
public :
    Son():Base1(0 ),Base2(0){
    }
    Son(int b ):Base1(b ),Base2(b){
    }
    Son(int a, int b ):Base1(b ), Base2(a){
    }
private :
    int m_B=2;

};

int main1(int argc, char *argv[])
{
    Son s ;
}
